package cn.bgs.service.impl;

import cn.bgs.dao.StuMapper;
import cn.bgs.pojo.*;
import cn.bgs.service.StuService;
import cn.bgs.utils.ExcelWriter;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;

    Logger log = LogManager.getLogger();

//    @Override
//    public List<Student> findAllStu(Student stu) {
//        List<Student> allStu = stuMapper.findAllStu(stu);
//        return allStu;
//    }

//    @Override
//    public List<StudentVo> fuzzyQueryStu(StudentVo stu) {
//        List<StudentVo> students = stuMapper.fuzzyQueryStu(stu);
//        return students;
//    }

    @Override
    public boolean delStu(Integer stuId) {
        return stuMapper.delStu(stuId);
    }




//    @Override
//    public List<Student> findAllStu() {
//        return stuMapper.findAllStu();
//    }

    @Override
    public List<Student> findAllStu(String stuName, String mobile, String sex, Integer age, String idNum) {
        return stuMapper.findAllStu(stuName,mobile,sex,age,idNum);
    }

    @Override
    public Student selOneStu(Integer stuId) {
        System.err.println("Service:::==========="+stuId);
        return stuMapper.selOneStu(stuId);
    }

    @Override
    public List<Provinces> selPro() {
        return stuMapper.selPro();
    }

    @Override
    public List<Cities> selCity() {
        return stuMapper.selCity();
    }

    @Override
    public List<Areas> selArea() {
        return stuMapper.selArea();
    }

    @Override
    public Integer updateStu(Student ss) {
        return stuMapper.updateStu(ss);
    }

    @Override
    public Integer insertStu(Student ss) {
        return stuMapper.insertStu(ss);
    }

    @Override
    public List<Student> selGraStu(String gradeId) {
        return stuMapper.selGraStu(gradeId);
    }

    @Override
    public List<Student> selDormStudent(String dormitoryId) {
        return stuMapper.selDormStudent(dormitoryId);
    }

    @Override
    public Student selStuInfo(String stuNum) {
        return stuMapper.selStuInfo(stuNum);
    }

    @Override
    public List<Student> addDormStu() {
        return stuMapper.addDormStu();
    }

    @Override
    public String stuExcel() {
        // 创建需要写入的数据列表
        List<Student> stu = stuMapper.stuExcel("","","",null,"");
        // 写入数据到工作簿对象内
        Workbook sheets = ExcelWriter.exportData(stu);

        // 以文件的形式输出工作簿对象
        FileOutputStream fileOut = null;
        try {
            String uuid = UUID.randomUUID().toString();//解决名称重复问题，用UUID来解决
            String replace = uuid.replace("-","");//去掉小横杠
            String name= replace.toUpperCase();//全部变大写
            String exportFilePath = "D:\\Email\\"+name+".xlsx";
            File exportFile = new File(exportFilePath);
            if (!exportFile.exists()) {
                exportFile.createNewFile();
            }

            fileOut = new FileOutputStream(exportFilePath);
            sheets.write(fileOut);
            fileOut.flush();
        } catch (Exception e) {
            log.info("输出Excel时发生错误，错误原因：" + e.getMessage());
        } finally {
            try {
                if (null != fileOut) {
                    fileOut.close();
                }
                if (null != sheets) {
                    sheets.close();
                }
            } catch (IOException e) {
                log.info("关闭输出流时发生错误，错误原因：" + e.getMessage());
            }
        }

        return "导出成功！";
    }

    @Override
    public void addStu(List<Student> readResult) {
        stuMapper.addStu(readResult);
    }
}
